package com.ustarlink.live.fragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;

import com.ustarlink.live.R;


public class VideoViewFragment extends Fragment {

    private String firstUrl = "http://www.cfm880.com/wp-content/uploads/2015/12/videoviewdemo.mp4?_=1";
    VideoView videoView;
    private String epgchannelUrl;
    private FragmentManager manager;
    private View view;
    public static final String TAG = "VideoViewFragment";

    public VideoViewFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view)
        {
            view = inflater.inflate(R.layout.fragment_video_view, container, false);
        }
        return view;
    }

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        videoView = (VideoView) view.findViewById(R.id.vv_live_container);
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        // OnKey is fired twice: the first time for key down, and the second time for key up
        getView().setOnKeyListener(new View.OnKeyListener() {
            boolean isShowEPG = true;

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() != KeyEvent.ACTION_DOWN)
                    return true;
                Log.v(String.valueOf(keyCode), TAG);
                if (keyCode == KeyEvent.KEYCODE_ENTER) {

                    manager = getFragmentManager();
                    if (isShowEPG || (manager.getBackStackEntryCount() == 0)) {
                        isShowEPG = false;
                        EPGFragment fragment = new EPGFragment();
                        manager.beginTransaction()
                                .addToBackStack(null)
                                .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                                .add(R.id.container, fragment, EPGFragment.TAG)
                                .commit();
                        Log.i("add EPGFragment", TAG);
                        return true;

                    } else {
                        isShowEPG = true;
                        manager.popBackStack();
                        Log.i("popBackStack", TAG);
                        EPGFragment epgFragment =
                                (EPGFragment) manager.findFragmentByTag(EPGFragment.TAG);
                        manager.beginTransaction()
                                .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                                .remove(epgFragment)
                                .commit();
                        Log.i("remove EPGFragment1", TAG);
                        return true;

                    }

                }

                if (keyCode == KeyEvent.KEYCODE_BACK){
                    EPGFragment epgFragment =
                            (EPGFragment) manager.findFragmentByTag(EPGFragment.TAG);

                    if (epgFragment != null){
                        manager.beginTransaction()
                                .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                                .remove(epgFragment)
                                .commit();
                        isShowEPG = true;
                        Log.i("remove EPGFragment2", TAG);
                    }

                }


                return false;
            }

        });


        if (view.isFocusable()){
            Log.i("is Focusable",TAG);
        } else {
            Log.i("isn't Focusable",TAG);
        }
        if (epgchannelUrl == null){
            Log.i(TAG+2, firstUrl);

            videoView.setVideoURI(Uri
                    .parse(firstUrl));
            videoView.start();
        } else {
            Log.i(TAG+1, epgchannelUrl);
            videoView.setVideoURI(Uri
                    .parse(epgchannelUrl));


        }


        videoView.setOnTouchListener(new View.OnTouchListener() {
            boolean check = true;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                manager = getFragmentManager();
                if (check || (manager.getBackStackEntryCount() == 0)){
                    check = false;
                    EPGFragment fragment = new EPGFragment();
                    manager.beginTransaction()
                            .addToBackStack(null)
                            .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                            .add(R.id.container, fragment, EPGFragment.TAG)
                            .commit();
                    Log.i("add EPGFragment", TAG);

                } else {
                    check = true;
                    manager.popBackStack();
                    Log.i("popBackStack", TAG);
                    EPGFragment epgFragment1 =
                            (EPGFragment) manager.findFragmentByTag(EPGFragment.TAG);
                    manager.beginTransaction()
                            .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                            .remove(epgFragment1)
                            .commit();
                    Log.i("remove EPGFragment", TAG);

                }
                return false;
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    public void getUrlFromEPG(String channelUrl){
        epgchannelUrl = channelUrl;
        Log.i(TAG+1, epgchannelUrl);

    }
}
